#include "../comm.h"
class Solution {
public:
    long long countAlternatingSubarrays(vector<int>& nums) {
        if(nums.size() == 1) return 1;
        long long ans = 0;
        // 找到每个最长的交替子数组
        int i = 0, j = 1;
        for(; j < nums.size(); ++j)
        {
            while(j < nums.size() && nums[j] != nums[j - 1]) ++j;
            long long tmp = j - i;
            ans += tmp * (tmp + 1) / 2ll;
            i = j;
        }
        if(i == nums.size() - 1)
            ++ans;
        return ans;
    }
};